using Consts;
using Dto;
using Model;
using Newtonsoft.Json;
using UI;
using UI.Msg;
using UnityEngine;

namespace Net.Handler
{
    public class MatchHandler : HandlerBase
    {
        public override void OnReceive(ResBase resBase)
        {
            switch (resBase.subCode)
            {
                case MatchCodeConst.ENTER:
                    EnterCallable(resBase);
                    break;
                case MatchCodeConst.ENTER_BRO:
                    EnterBROCallable(resBase);
                    break;
                case MatchCodeConst.LEAVE_BRO:
                    LeaveBROCallable(resBase);
                    break;
                case MatchCodeConst.READY_BRO:
                    ReadyBROCallable(resBase);
                    break;
                case MatchCodeConst.START_PRO:
                    StartBRO();
                    break;
                default:
                    break;
            }
        }

        /**
         * 游戏开始广播处理
         */
        private void StartBRO()
        {
            // 开始游戏
            PromptMsg promptMsg = new PromptMsg("所有玩家准备开始游戏", Color.blue);
            Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, promptMsg);
            // 隐藏状态面板的准备文字
            Dispatch(AreaCode.UI, UIEvent.PLAYER_HIDE_STATE, null);
        }


        /**
         * 准备广播通知
         */
        private void ReadyBROCallable(ResBase resBase)
        {
            string readUserId = resBase.result.ToString();

            GameModel gameModel = Models.GameModel;
            gameModel.MatchRoom.Ready(readUserId);
            // 更新玩家的准备状态
            Dispatch(AreaCode.UI,UIEvent.PLAYER_READY,readUserId);

            if (readUserId == Models.GameModel.Account.id)
            {
                // 发送消息 隐藏准备按钮,防止多次点击 和服务器交互
                Dispatch(AreaCode.UI,UIEvent.PLAYER_HIDE_READY_BUTTON,null);
            }
        }

        /**
         * 离开广播通知
         */
        private void LeaveBROCallable(ResBase resBase)
        {
            string leaveUserId = resBase.result.ToString();
            Dispatch(AreaCode.UI,UIEvent.PLAYER_LEAVE,leaveUserId);
            
            ResetPosition();
            
            GameModel gameModel = Models.GameModel;
            gameModel.MatchRoom.RemoveUserById(leaveUserId);
            
        }

        /**
         * 进入广播通知
         */
        private void EnterBROCallable(ResBase resBase)
        {
            Account account = JsonConvert.DeserializeObject<Account>(resBase.result.ToString());

            // Dispatch(AreaCode.UI,UIEvent.PLAYER_ENTER,account.id);

            GameModel gameModel = Models.GameModel;
            gameModel.MatchRoom.AddUser(account);

            ResetPosition();
            
            if ( gameModel.MatchRoom.LeftId != null)
          {
              Account leftUserDto = gameModel.MatchRoom.uids[gameModel.MatchRoom.LeftId];
              Dispatch(AreaCode.UI,UIEvent.SET_LEFT_PLAYER_DATA,leftUserDto);
          }
          if ( gameModel.MatchRoom.RightId != null)
          {
              Account rightUserDto = gameModel.MatchRoom.uids[gameModel.MatchRoom.RightId];
              Dispatch(AreaCode.UI,UIEvent.SET_RIGHT_PLAYER_DATA,rightUserDto);
          }
          
          // 绑定自身数据

            
            Dispatch(AreaCode.UI,UIEvent.PLAYER_ENTER,account.id);

            PromptMsg promptMsg = new PromptMsg("有新玩家 (" + account.nickName + ") 进入", Color.blue);
            Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, promptMsg);
        }

        /**
         * 确认进入回调 
         */
        private void EnterCallable(ResBase resBase)
        {
            if (resBase.resCode == 200)
            {
                MatchRoomDto matchRoomDto = JsonConvert.DeserializeObject<MatchRoomDto>(resBase.result.ToString());
                GameModel gameModel = Models.GameModel;
                gameModel.MatchRoom = matchRoomDto;


                ResetPosition();

          

                Dispatch(AreaCode.UI, UIEvent.SHOW_ENTER_ROOM_BUTTON, null);
            }
        }

        private void ResetPosition()
        {
            GameModel gameModel = Models.GameModel;
            
            gameModel.MatchRoom.ResetPosition(gameModel.Account.id);
            

            // 显示现在房间内的玩家
            /*if ( gameModel.MatchRoom.LeftId != null)
            {
                Account leftUserDto = gameModel.MatchRoom.uids[ gameModel.MatchRoom.LeftId];
                Dispatch(AreaCode.UI,UIEvent.SET_LEFT_PLAYER_DATA,leftUserDto);
            }
            if ( gameModel.MatchRoom.RightId != null)
            {
                Account rightUserDto = gameModel.MatchRoom.uids[ gameModel.MatchRoom.RightId];
                Dispatch(AreaCode.UI,UIEvent.SET_RIGHT_PLAYER_DATA,rightUserDto);
            }*/
        }
    }
}